//
//  AccountAddView.m
//  iMoney
//
//  Created by Sean on 3/31/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AccountAddView.h"
#import "Account.h"
#import "iMoneyAppDelegate.h"

@implementation AccountAddView


- (IBAction) closeAdd: (UIButton*) sender
{
	//we have a add
	if(sender.tag == 11)
	{
		Account* newAccount = [[Account alloc] initWithNameOffsetAmount:namef.text :[amountf.text doubleValue]];
		[[iMoneyAppDelegate getMoneyState] addAccount:newAccount];
		[newAccount release];
		
		[mainTableReload reloadData];
	}
	
	[((iMoneyAppDelegate*)[[UIApplication sharedApplication] delegate]) toggleTabBar];
	[((iMoneyAppDelegate*)[[UIApplication sharedApplication] delegate]) toggleNavBar];
	
	[self removeFromSuperview];
}




- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       
		UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradientBackground.png"]];
		[self addSubview:background];
		[self sendSubviewToBack:background];
		[background release];
		
		
		UILabel *name = [[UILabel alloc ] initWithFrame:CGRectMake(5, 10, 310, 25)];
		name.textAlignment =  UITextAlignmentLeft;
		name.textColor = [UIColor whiteColor];
		name.backgroundColor = [UIColor clearColor];
		name.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(18.0)];
		name.text = @"Account Name:";
		
		namef = [[UITextField alloc] initWithFrame:CGRectMake(5, 50, 180, 25)];
		namef.borderStyle =  UITextBorderStyleLine;
		namef.delegate = self;
		[namef setBackgroundColor:[UIColor whiteColor]];
		
		UILabel *amount = [[UILabel alloc ] initWithFrame:CGRectMake(5, 100, 310, 25)];
		amount.textAlignment =  UITextAlignmentLeft;
		amount.textColor = [UIColor whiteColor];
		amount.backgroundColor = [UIColor clearColor];
		amount.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(18.0)];
		amount.text = @"Starting Amount:";
		
		
		amountf = [[UITextField alloc] initWithFrame:CGRectMake(5, 150, 100, 25)];
		amountf.delegate = self;
		//amountf.keyboardType = UIKeyboardTypeNumberPad;
		amountf.placeholder = @"0.0";
		amountf.borderStyle =  UITextBorderStyleLine;
		[amountf setBackgroundColor:[UIColor whiteColor]];
		
		UIButton *cancel = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
		cancel.frame = CGRectMake(10, 320, 100, 30);
		[cancel setTitle:@"Cancel" forState:UIControlStateNormal];
		cancel.backgroundColor = [UIColor clearColor];
		[cancel setTitleColor:[UIColor grayColor] forState:UIControlStateNormal ];
		[cancel setTag:10];
		
		UIButton *done = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
		done.frame = CGRectMake(200, 320, 100, 30);
		[done setTitle:@"Add" forState:UIControlStateNormal];
		done.backgroundColor = [UIColor clearColor];
		[done setTitleColor:[UIColor grayColor] forState:UIControlStateNormal ];
		[done setTag:11];
		
		
		[cancel addTarget:self action:@selector(closeAdd:) forControlEvents:UIControlEventTouchUpInside];
		[done addTarget:self action:@selector(closeAdd:) forControlEvents:UIControlEventTouchUpInside];
		
		[self addSubview:name];
		[self addSubview:namef];
		[self addSubview:amount];
		[self addSubview:amountf];
		[self addSubview:cancel];
		[self addSubview:done];
		
		
		[name release];
		[amount release];		
		
    }
    return self;
}

-(id) initWithFrameAndTable:(CGRect)frame:(UITableView*)mainTable
{
	mainTableReload = mainTable;
	return [self initWithFrame:frame];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return NO;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)dealloc {

	//[namef release];
	//[amountf release];
	[super dealloc];
}


@end
